/*	kd:
	This allows us to time the shop animation and events, when the layout
	finished its animation or is just starting off. You can also use the timer
	as a means of running the animation. */

struct Gcs_ShopSequencer play {
	uint m_tics;
	uint m_wait_tics;
	bool m_active;
	bool m_is_sanitary_anim;
	anim_state m_anim_state;
	Gc_Shop m_shop;
	
	enum anim_state {
		ANIM_IN = 0,
		ANIM_OUT
	}
	
	void Init (Gc_Shop shop) {
		m_shop        = shop;
		m_active      = false;
		m_tics        = 0;
		m_anim_state  = ANIM_IN;
		m_wait_tics   = 0;
	}
	
	void GoOut (uint tics, bool shop_closes = false) {
		/*	Reverse the animation if we're already there. */
		if(m_active) {
			if(m_anim_state == ANIM_IN) {
				m_wait_tics = m_tics;
				m_tics = 0;
			}
			
			else if(m_anim_state == ANIM_OUT) {
				m_shop.OnAnimOutContinue();
			}
		}
		
		else {
			m_wait_tics = tics;
			m_tics = 0;
			m_active = true;
		}
		
		if(shop_closes) {
			m_shop.OnShopAnimOutStart();
			m_is_sanitary_anim = true;
		}
		
		else if(m_anim_state != ANIM_OUT) {
			m_shop.OnAnimOutStart();
			m_is_sanitary_anim = false;
		}
		
		m_anim_state = ANIM_OUT;
	}
	
	void GoIn (uint tics, bool shop_opens = false) {
		if(m_active) {
			if(m_anim_state == ANIM_OUT) {
				m_wait_tics = m_tics;
				m_tics = 0;
			}
			
			else if(m_anim_state == ANIM_IN) {
				m_shop.OnAnimInContinue();
			}
		}
		
		else {
			m_wait_tics = tics;
			m_tics = 0;
			m_active = true;
		}
		
		if(shop_opens) {
			m_shop.OnShopAnimInStart();
			m_is_sanitary_anim = true;
		}
		
		else if(m_anim_state != ANIM_IN) {
			m_shop.OnAnimInStart();
			m_is_sanitary_anim = false;
		}
		
		m_anim_state = ANIM_IN;
	}
	
	void TickOps () {
		if(!m_active) {
			return;
		}
		
		m_tics++;
		
		if(m_wait_tics <= m_tics) {
			m_active = false;
			m_tics = 0;
			m_wait_tics = 0;
			
			if(m_anim_state == ANIM_IN) {
				if(m_is_sanitary_anim) {
					m_shop.OnShopAnimInDone();
				}
				
				else {
					m_shop.OnAnimInDone();
				}
			}
			
			else if(m_anim_state == ANIM_OUT) {
				if(m_is_sanitary_anim) {
					m_shop.OnShopAnimOutDone();
				}
				
				else {
					m_shop.OnAnimOutDone();
				}
			}
			
			m_is_sanitary_anim = false;
		}
	}
}